package main

import (
	"fmt"
	config "gitee.com/lanshiren/miniprogram/app/conf"
	"gitee.com/lanshiren/miniprogram/app/handler"
	"gitee.com/lanshiren/miniprogram/app/log"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"os"
	"os/signal"
	"syscall"
	"time"
)

var banner = `
.____       _____    _______    _________ ___ ___ .________________________ _______   
|    |     /  _  \   \      \  /   _____//   |   \|   \______   \_   _____/ \      \  
|    |    /  /_\  \  /   |   \ \_____  \/    ~    \   ||       _/|    __)_  /   |   \ 
|    |___/    |    \/    |    \/        \    Y    /   ||    |   \|        \/    |    \
|_______ \____|__  /\____|__  /_______  /\___|_  /|___||____|_  /_______  /\____|__  /
        \/       \/         \/        \/       \/             \/        \/         \/ 
`

func main() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)
	signal.Notify(c, syscall.SIGTERM)
	// config init
	conf, err := config.LoadConfig("conf/app.toml")
	if err != nil {
		log.MainLogger.Error(fmt.Sprintf("read ./coding.toml error! err: %v", err.Error()))
		os.Exit(-1)
	}
	// mysql init
	db, err := gorm.Open("mysql", conf.MysqlUrl)
	if err != nil {
		log.MainLogger.Fatal(fmt.Sprintf("mysql url=[%s] is error=[%v]", conf.MysqlUrl, err))
		return
	}
	defer db.Close()
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)
	db.LogMode(true)
	config.SetDB(db)
	go func() {
		// heartbeat
		for {
			total := 0
			db.Raw("select 1").Row().Scan(&total)
			time.Sleep(20 * time.Second)
		}
	}()

	// scheduler
	//sch := service.NewScheduler()
	//sch.AddTask(1000, service.FetchStatusCycle)
	//sch.RunAllTasks()

	fmt.Print(banner)
	log.MainLogger.Info(banner)
	log.MainLogger.Info(fmt.Sprintf("app start, listen on %s", conf.Listen))
	r := gin.Default()

	r.POST("/api/v1/wx_login", handler.HandleAuthCode)
	r.Use(handler.Authorize())
	r.POST("/api/v1/auth_login", handler.AuthorizedLogin)
	r.GET("/api/v1/mysql_qps_monitor", handler.HandleMonitorMysqlQPS)
	r.GET("/api/v1/mysql_global_status", handler.HandleQueryMySQLGlobalStatus)
	r.POST("/api/v1/create_activity", handler.HandleCreateActivity)
	r.GET("/api/v1/get_activities", handler.HandleSearchActivities)
	r.GET("/api/v1/get_activity", handler.HandleGetActivity)
	r.PUT("/api/v1/update_activity", handler.HandleUpdateActivity)
	r.PUT("/api/v1/verify_activity", handler.HandleVerifyActivity)
	r.POST("/api/v1/registration_activity", handler.HandleRegistrationActivity)
	r.POST("/api/v1/cancel_registration_activity", handler.HandleCancelRegistrationActivity)
	r.GET("/api/v1/get_contacts", handler.HandleSearchContacts)
	r.PUT("/api/v1/update_userinfo", handler.UpdateUserInfo)
	r.GET("/api/v1/search_contact", handler.HandleSearchContact)
	r.PUT("/api/v1/update_contact_role", handler.HandleUpdateContactRole)
	r.GET("/api/v1/get_access_key", handler.GetAccessKey)
	r.GET("/api/v1/get_activity_users", handler.HandleGetActivityUsers)

	fmt.Printf("%v", conf)
	go func() {
		<-c
		//sch.StopAllTasks()
		os.Exit(1)
	}()

	if err := r.Run(conf.Listen); err != nil {
		os.Exit(-1)
	}
}
